/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#ifndef __COMMAND_LINE_HH__
#define __COMMAND_LINE_HH__

#include <boost/optional.hpp>
#include <cstdlib>
#include <iostream>
#include <stdint.h>
#include <string>
#include <utility>

#include "regressor.hh"

namespace flassol
{
inline boost::optional<std::pair<uint8_t, uint8_t> >
parse_quadratic (const std::string& s)
{
  boost::optional<std::pair<uint8_t, uint8_t> > rv;

  if (s.size () == 2)
    {
      rv = std::make_pair (static_cast<uint8_t> (s[0]),
                           static_cast<uint8_t> (s[1]));
    }
  else
    {
      std::cerr << "ERROR: bad quadratic spec '" << s << "'" << std::endl;
    }

  return rv;
}

inline boost::optional<std::pair<uint8_t, uint8_t> >
parse_dotproduct (const std::string& s)
{
  boost::optional<std::pair<uint8_t, uint8_t> > rv;

  if (s.size () == 2 && s[0] != s[1])
    {
      rv = std::make_pair (static_cast<uint8_t> (s[0]),
                           static_cast<uint8_t> (s[1]));
    }
  else
    {
      std::cerr << "ERROR: bad dotproduct spec '" << s << "'" << std::endl;
    }

  return rv;
}

inline boost::optional<std::pair<uint8_t, Regressor::NGramSpec> >
parse_ngram (const std::string& s)
{
  boost::optional<std::pair<uint8_t, Regressor::NGramSpec> > rv;

  if (s.size () > 1)
    {
      char *endptr;
      unsigned int n = strtoul (s.c_str () + 1, &endptr, 0);
      unsigned int k = 0;

      if (*endptr == ',')
        {
          k = strtoul (endptr + 1, &endptr, 0);
        }

      if (*endptr == '\0' && n > 1 && n < 16 && k < 16)
        {
          Regressor::NGramSpec spec (n, k);
          rv = std::make_pair (static_cast<uint8_t> (s[0]), spec);
        }
    }

  if (! rv)
    {
      std::cerr << "ERROR: bad ngram spec '" << s << "'" << std::endl;
    }

  return rv;
}

}

#endif // __COMMAND_LINE_HH__
